class Front::HomeController < Front::BaseController
  # 注册用户
  # params[:phone :password :photo]
  # 路由 users/sign_up
  def sign_up
    @user = User.new phone: params[:phone], password: params[:password],photo: params[:photo]
    @user.email = params[:phone] + "@qq.com"
    if @user.save
      render json: {message: '注册成功'}, status: :ok
    else
      render json: @user.errors, status: :bad_request
    end
  end

  # 登陆
  # params[:login :password]
  # 路由 users/validate
  def validate!
    @user = User.find_for_database_authentication(login: params[:login].downcase)
    return render json: {message: '没用该用户'}, status: :bad_request unless @user
    if @user.valid_password?(params[:password])
      @user.save
    else
      return render json: {message: '密码错误'}, status: :bad_request
    end
    render json: @user
  end

  # 获取当前用户对象
  def current
    render json: current_user
  end

  # 修改用户资料
  # params[:id :name :sex :photo :phone :email :password :school]
  # 路由 users/update_user_info
  def update_user_info
    @user = User.find_by_id(params[:id])
    if @user.present?
      u_params = user_params
      if u_params[:password].blank?
        u_params.delete(:password)
      else
        u_params[:password_confirmation] = u_params[:password]
      end
      if @user.update_attributes(u_params)
        render json:{message:'修改成功'} ,status: :ok
      else
        render json:{message:'修改参数有误'}
      end
    else
      render json:{message:'无对应ID'}
    end
  end

  # 通过用户id获取用户
  # params[:id]
  # 路由 users/get_user_by_id
  def get_user_by_id
    @user = User.find_by_id(params[:id])
    if @user.present?
      render json:{user:@user} , status: :ok
    else
      render json:{}
    end
  end

  # 用户参数
  def user_params
    params.permit(:id ,:name ,:sex ,:photo ,:phone ,:email ,:password ,:school)
  end

  #  新增文章
  # params[:publish_id :content :picture :praise_num :comment_num :state]
  # 路由 articles/new_article
  def new_article
    @article = Article.new publish_id:params[:publish_id], content:params[:content], picture:params[:picture],
                           praise_num:params[:praise_num], comment_num:params[:comment_num], state:params[:state]

    if @article.save
      render json: {message:'文章保存成功'} , status: :created
    else
      render json: {message:'保存出错,参数或数据有误'} , status: :bad_request
    end
  end


  # 获取所有文章
  # 路由 articles/get_all_articles
  def get_all_articles
    @articles = Article.all
    if @articles.present?
      render json:{articles:@articles}
    else
      render json:{}
    end
  end

  # 获取对应学校所有文章
  # params[:school] 学校名
  # 路由 articles/get_all_articles_by_school
  def get_all_articles_by_school
    user_ids = User.where('school=?',params[:school]).ids
    @articles = Article.where(id: user_ids)
    if @articles
      render json:{articles:@articles}
    else
      render json:{}
    end
  end

  # 获取对应用户所有文章
  # params[:publish_id] 用户ID
  # 路由 articles/get_all_articles_by_publish_id
  def get_all_articles_by_publish_id
    @articles = Article.find_by_publish_id(params[:publish_id])
    if @articles
      render json:{articles:@articles}
    else
      render json:{}
    end
  end

  # 获取单个文章信息
  # params[:id] 文章ID
  # 路由 articles/get_article
  def get_article
    @article = Article.find_by_id(params[:id])
    if @article
      render json:{article:@article}
    else
      render json:{}
    end
  end

  # 文章点赞
  # params[:id] 文章id
  # 路由 articles/article_praise
  def article_praise
    @article = Article.find_by_id(params[:id])
    if @article.present?
      @article.update_attributes(praise_num:@article.praise_num + 1)
      render json:{message:'点赞成功'} , status: :ok
    else
      render json:{message:'没有对应的文章'}
    end
  end


  # 新增评论
  # params[:owner_id :owner_type(Article,Comment) :publish_id :content :state]
  # 路由 comments/new_comment
  def new_comment
    @comment = Comment.new owner_id:params[:owner_id] ,owner_type:params[:owner_type],
                           publish_id:params[:publish_id], content:params[:content], state:params[:state]
    if @comment.save
      if params[:owner_type] == 'Article'
        @article = Article.find_by_id(params[:owner_id])
        @article.update_attributes(comment_num:@article.comment_num + 1)
      end
      render json: {message:'评论保存成功'} , status: :created
    else
      render json: {message:'保存出错,参数或数据有误'} , status: :bad_request
    end
  end

  # 获取文章评论
  # params[:article_id]
  # 路由 comments/article_comment
  def article_comment
    @comments_by_article = Comment.where('owner_id=? && owner_type=?',params[:article_id],'Article').order(created_at: :desc)
    comments_ids = []
    @comments_by_article.each do |item|
      # 递归查找评论
      find_next_comment(item,comments_ids)
    end
    @comments = Comment.where(id: comments_ids)

    if @comments.present?
      render json: {comments:@comments} , status: :created
    else
      render json: {} , status: :bad_request
    end
  end

  # 递归查找下面一条评论,为空返回数组,有的话查找下一条
  def find_next_comment(comment,comments_ids)
    if comment.present?
      comments_ids << comment.id
      find_next_comment(Comment.where('owner_id=? && owner_type=?',comment.id,'Comment').first,comments_ids)
    else
      return comments_ids
    end
  end


  # 新增好友
  # params[:a_id :b_id]
  # 路由 friends/new_friend
  def new_friend
    # 两个id按照大小放入数据库
    max = params[:a_id] > params[:b_id] ? params[:a_id].to_i : params[:b_id].to_i
    min = params[:a_id] > params[:b_id] ? params[:b_id].to_i : params[:a_id].to_i
    @user_association = UserAssociation.new a_user:min , b_user:max

    if @user_association.save
      render json: {message: '新增成功'} ,status: :created
    else
      render json: {message: '新增出错'} , status: :bad_request
    end
  end

  # 获取用户好友
  # params[:user_id]
  # 路由 friends/get_friend
  def get_friend
    @users = User.where(id: UserAssociation.where(a_user:params[:user_id]).ids)
    if @users.present?
      render json: {friends:@users}
    else
      render json: {}
    end
  end

end
